<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/11/29 0029
 * Time: 0:41
 */

namespace logicmodel;

use comservice\Response;
use datamodel\AwardRecord;
use datamodel\Goods;
use datamodel\GoodsManage;
class ManageLogic
{

    /**
     * 代理作品
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function manageGoods($uid){
        $where['gm.uid'] = $uid;
        $where['gm.is_del'] = 0;
        $field = ['g.name','g.image','g.id','g.price','g.desc'];
        $data =  (new GoodsManage())->alias('gm')
            ->join('goods g','g.id = gm.goods_id')
            ->where($where)
            ->order(['gm.id desc'])
            ->field($field)
            ->select();
        if($data){
            $data = collection($data)->toArray();
            return Response::success('success',$data);
        }
        return Response::success('暂未代理商品',[]);
    }

    /**
     * 代理作品详情
     * @param $id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsDetail($id){
        $where['is_del'] = 0;
        $where['is_show'] = 1;
        $where['id'] = $id;
        $field = ['*'];
        $data = (new Goods())->where($where)->order(['order asc'])
            ->field($field)
            ->find();
        if(empty($data)) return Response::fail('作品信息错误');
        $data = $data->toArray();
        $share_url = config('site.manage_share_url');
        $data['share_url'] = $share_url.'/#/manage_goods?goods_num='.$data['goods_num'];
        return Response::success('success',$data);
    }
    /**
     * 收益记录
     * @param $uid
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsAward($uid,$id){
        $data = (new AwardRecord())->alias('ar')
            ->join('users u','u.id = ar.from_uid')
            ->field(['u.nick_name','u.head_image','ar.create_time','ar.account','ar.remark'])
            ->where(['ar.uid'=>$uid,'ar.goods_id'=>$id])
            ->order(['ar.id desc'])
            ->select();
        if($data){
            $data = collection($data)->toArray();
            return Response::success('success',$data);
        }
        return Response::success('暂无数据',[]);
    }
}